package cn.zxl.spring.holiday.job.web.controller.dictionary;


import cn.zxl.spring.holiday.job.domain.DictData;
import cn.zxl.spring.holiday.job.domain.DictType;
import cn.zxl.spring.holiday.job.service.DictDataService;
import cn.zxl.spring.holiday.job.service.DictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 字典数据表 前端控制器
 * </p>
 *
 * @author bofeifei
 * @since 2021-10-06
 */
@Controller
@RequestMapping("/dictData")
public class DictDataController {

    @Autowired
    private DictDataService dataService;
    @Autowired
    private DictTypeService dictTypeService;

    @RequestMapping("/{dictType}")
    public String dictData(@PathVariable("dictType") String dictType, Model model) {
        DictType type = dictTypeService.selectOneByType(dictType);
        model.addAttribute("type",type);
        List<DictData> dictData = dataService.selectByType(dictType);
        model.addAttribute("dictDataList",dictData);
        return "dictdata/view";
    }

    @RequestMapping("/edit/{dictCode}")
    public String edit(@PathVariable("dictCode")String dictCode,Model model) {
        List<DictType> dictTypes = dictTypeService.selectByDictName();
        model.addAttribute("dictTypes",dictTypes);
        if (dictCode != null) {
            DictData dictData = dataService.selectOneByCode(dictCode);
            model.addAttribute("DictData", dictData);
        }
        return "dictdata/edit";
    }

    @RequestMapping("/add")
    public String add(String dictType,Model model) {

        List<DictType> dictTypes = dictTypeService.selectByDictName();
        model.addAttribute("dictTypes",dictTypes);
        model.addAttribute("dictTypeAdd", dictType);
        return "dictdata/edit";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(DictData dictData) {
        String dictType = dictData.getDictType();
        if (dictData.getDictCode() != null) {
            dataService.updateById(dictData);
            return "redirect:/dictData/" + dictType;
        }
        dataService.save(dictData);
        return "redirect:/dictData/" + dictType;
    }
}

